const myData = {
    id: '',
    init() {
        var url = window.location.href;
        myData.id = myData.baseUrl_query(url, 'id');
    },
    bindEvent() {
        console.log(myData.id)
        this.findId();
        this.update();
    },
    baseUrl_query(url, name) {
        //获取跳转页面参数
        var pattern = new RegExp("[?&]" + name + "=([^&]+)", "g");
        var matcher = pattern.exec(url);
        var items = null;
        if (null != matcher) {
            try {
                items = decodeURIComponent(decodeURIComponent(matcher[1]));
            } catch (e) {
                try {
                    items = decodeURIComponent(matcher[1]);
                } catch (e) {
                    items = matcher[1];
                }
            }
        }
        return items;
    },
    findId() {
        $.ajax({
            type: 'get',
            url: 'detail/findById',
            data: { id: myData.id },
            success: function(res) {
                console.log(res)
                if (res.status == 200) {
                    var reData = res.list;
                    $('#firstname').attr('data-id', reData[0].id)
                    $('#firstname').val(reData[0].name);
                    $('#lastname').val(reData[0].age);
                }
            },
            error: function(err) {
                console.log(err)
            }
        })

    },
    update() {
        $(document).delegate('#btnSave', 'click', function() {
            var param = {};
            param.id = Number($('#firstname').attr('data-id'));
            param.name = $('#firstname').val();
            param.age = $('#lastname').val();
            $.ajax({
                type: 'get',
                url: 'detail/update',
                data: param,
                success: function(res) {
                    if (res.status == 200) {
                        window.location.href = 'users'
                    } else {
                        alert('修改失败')
                    }
                },
                error: function(err) {
                    console.log(err)
                }
            })
        })

    }
}
myData.init();
myData.bindEvent();